package net.gooduo.spring_boot_template.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.gooduo.spring_boot_template.dto.PageDto;
import net.gooduo.spring_boot_template.exception.HttpExceptionEnum;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class HttpResult {
    private Integer code;
    private String msg;
    private Object data;

    public static HttpResult ok() {
        return new HttpResult(0, "ok", null);
    }

    public static HttpResult data(Object data) {
        HttpResult r = HttpResult.ok();
        r.setData(data);
        return r;
    }

    public static HttpResult error(HttpExceptionEnum e) {
        return new HttpResult(e.getCode(), e.getMsg(), null);
    }

    public static HttpResult page(IPage page) {
        PageDto pageDto = new PageDto();

        pageDto.setPage(page.getCurrent());
        pageDto.setCount(page.getTotal());
        pageDto.setSize(page.getSize());
        pageDto.setList(page.getRecords());

        return HttpResult.data(pageDto);
    }
}
